import {Component, EventEmitter, OnInit,Output} from '@angular/core';
import {Vehicle} from "../../shared/model/vehicle";
import {ActivatedRoute, Router} from "@angular/router";
import {CamManageService} from "../../shared/service/cam-manage.service";
import {SingleOpenWinService} from "../../shared/service/single-open-win.service";

@Component({
  selector: 'app-cam-manage-list',
  templateUrl: './cam-manage-list.component.html',
  styleUrls: ['./cam-manage-list.component.css']
})
export class CamManageListComponent implements OnInit {

  vehicles:Vehicle[];
  oldVehicles: Vehicle[];
  selectedVehicle: Vehicle;




  constructor(
    private router:Router,
    private camManageService:CamManageService,
    private singleOpenWinService:SingleOpenWinService
  ) { }

  ngOnInit() {
    this.getVehicles();

    //筛选符合搜索框中条件的RoomList集合
    this.camManageService.searchEvent.subscribe(param =>{
      let result = this.oldVehicles;
      if(param){
        result = result.filter((r:Vehicle)=>r.name.indexOf(param)!=-1);
      }
      this.vehicles = result;
    });

    this.camManageService.listFreshEvent.subscribe(
      param => {
        if (param) {
          this.getVehicles();
        }
      }
    );
  }

  getVehicles(){
    this.camManageService.getVehicle().then(
      res => {
        this.vehicles = res;
        this.oldVehicles = res;
      }
    );
  }

  onSelect(vehicle:Vehicle){
    this.selectedVehicle = vehicle;
  }

  delVehicle(vehicle:Vehicle){
    if(confirm("确认删除?")){
      this.camManageService.deleteVehicle(vehicle.oid).then(
        res => {
          alert(res.message);
          this.singleOpenWinService.oidChange.emit("new");
          this.router.navigateByUrl('/camManage/new');
          this.camManageService.searchFreshEvent.emit("delete");
          this.getVehicles();
        }
      );
    }
  }
}
